﻿using AutoMapper;
using Domain.Factory_Modeling;
using Infratructure.Interface;
using MediatR;
using MES.Server.API.Dto.Factory_Modeling.OutPut;
using ReturnValue;
using System;

namespace MES.Server.API.Application.Command.Factory_Modeling
{
    public class GetProductionLineHander : IRequestHandler<GetProductionLineCommand, ApiPaging<GetProductionLineOutPut>>
    {
        private readonly IRepository<ProductionLineModel> productionLineRepo;
        private readonly IRepository<WorkShopModel> workShopRepo;
        private readonly IMapper mapper;
        public GetProductionLineHander(IRepository<ProductionLineModel> productionLineRepo, IMapper mapper, IRepository<WorkShopModel> workShopRepo)
        {
            this.productionLineRepo = productionLineRepo;
            this.mapper = mapper;
            this.workShopRepo = workShopRepo;
        }
        /// <summary>
        /// 产线信息
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public Task<ApiPaging<GetProductionLineOutPut>> Handle(GetProductionLineCommand request, CancellationToken cancellationToken)
        {
            var result = new ApiPaging<GetProductionLineOutPut>();
            var productionLine = productionLineRepo.GetAll().Where(x => x.IsDel == false);
            var workShop = workShopRepo.GetAll().Where(x => x.IsDel == false);
            var linq = from p in productionLine
                       join w in workShop on p.WorkShopId equals w.Id
                       select new GetProductionLineOutPut
                       {
                            Id=p.Id,
                            ProductionLineCode  = p.ProductionLineCode,
                            ProductionLineName= p.ProductionLineName,
                            WorkShopId=w.Id,
                            WorkShopName=w.WorkShopName,
                            ProductionLineDesc=p.ProductionLineDesc,
                            CreaterPeople= p.CreaterPeople,
                            CreateTime= p.CreateTime,
                           UpdateTime = p.UpdateTime ?? DateTime.Now
                       };
            result.TotalPage = linq.OrderByDescending(x=>x.CreateTime). Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize).ToList();
            result.Totalcount = linq.Count();
            result.Code = StatusCode.Success;
            return Task.FromResult(result);

        }
    }
}
